package com.sitech.ibnms.util;

import java.util.Random;

/**
 * 
 * <p>
 * Title: RandomUtil
 * </p>
 * <p>
 * Description: 随机类方法
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company: SI-TECH
 * </p>
 * 
 * @author xuyw
 * @version 1.0
 * @createtime Sep 25, 2010 10:25:27 AM
 * 
 */
public class RandomUtil
{

	public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	public static final String numberChar = "0123456789";

	public static final int RAND_SIZE = 20;

	/**
	 * 返回一个定长的随机字符串(只包含大小写字母、数字)
	 * 
	 * @param length 字符串的长度
	 * @return length长度的随机字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:30:05 PM
	 */

	public static String generateString(int length)
	{

		StringBuffer sb = new StringBuffer();

		Random random = new Random();

		for (int i = 0; i < length; i++)
		{

			sb.append(allChar.charAt(random.nextInt(allChar.length())));

		}

		return sb.toString();

	}

	/**
	 * 返回一个定长的随机纯字母字符串(只包含大小写字母)
	 * 
	 * @param length 字符串的长度
	 * @return length长度的随机字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:31:01 PM
	 */

	public static String generateMixString(int length)
	{

		StringBuffer sb = new StringBuffer();

		Random random = new Random();

		for (int i = 0; i < length; i++)
		{

			sb.append(letterChar.charAt(random.nextInt(letterChar.length())));

		}

		return sb.toString();

	}

	/**
	 * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
	 * 
	 * @param length 字符串的长度
	 * @return length长度的随机字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:32:02 PM
	 */

	public static String generateLowerString(int length)
	{

		return generateMixString(length).toLowerCase();

	}

	/**
	 * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
	 * 
	 * @param length 字符串的长度
	 * @return length长度的随机字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:32:42 PM
	 */

	public static String generateUpperString(int length)
	{

		return generateMixString(length).toUpperCase();

	}

	/**
	 * 生成一个定长的纯0字符串
	 * 
	 * @param length 字符串的长度
	 * @return length长度的随机字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:32:59 PM
	 */

	public static String generateZeroString(int length)
	{

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < length; i++)
		{

			sb.append('0');

		}

		return sb.toString();

	}

	/**
	 * 生成一个定长的纯str字符串，size个str
	 * eg：length=3;str="abc"; 那么结果就是"abcabcabc"
	 * 
	 * @param length str重复的次数
	 * @param str 指定的字符串
	 * @return length个str字符串的组合
	 * 
	 * @author jiayb Mar 18, 2010 8:45:27 PM
	 */
	public static String generateStrString(int length, String str)
	{

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < length; i++)
		{

			sb.append(str);

		}

		return sb.toString();

	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0
	 * eg:num=123;fixdlenth=5; 结果是00123;
	 * 
	 * @param num 初始数字 为long类型
	 * @param fixdlenth 定长长度
	 * @return 定长的字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:33:26 PM
	 */

	public static String toFixdLengthString(long num, int fixdlenth)
	{

		StringBuffer sb = new StringBuffer();

		String strNum = String.valueOf(num);

		if (fixdlenth - strNum.length() >= 0)
		{

			sb.append(generateZeroString(fixdlenth - strNum.length()));

		}
		else
		{

			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异    常!");

		}

		sb.append(strNum);

		return sb.toString();

	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0
	 * eg:num=123;fixdlenth=5; 结果是00123;
	 * @param num 初始数字 
	 * @param fixdlenth 定长长度
	 * @return 定长的字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:34:30 PM
	 */

	public static String toFixdLengthString(int num, int fixdlenth)
	{

		StringBuffer sb = new StringBuffer();

		String strNum = String.valueOf(num);

		if (fixdlenth - strNum.length() >= 0)
		{

			sb.append(generateZeroString(fixdlenth - strNum.length()));

		}
		else
		{

			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异    常!");

		}

		sb.append(strNum);

		return sb.toString();

	}

	/**
	 * generateNumStrFromMinToMax,产生min-max之间的数字
	 * 
	 * 
	 * 例子->
	 * 产生1000到9999的随机数
	 * Long tempLong; //不能设定为int,必须设定为long
	 * tempLong=Math.round(Math.random()*8999+1000);
	 * tempLong = Math.round(Math.random() * 3 + 1);
	 * return
	 * @param min 最小值
	 * @param max 最大值
	 * @return 定长的字符串
	 * 
	 * @author jiayb Mar 18, 2010 8:34:30 PM
	 */
	public static String generNumStrBetween(long min, long max)
	{
		return String.valueOf(Math.round(Math.random() * (max - min) + min));
	}
}
